#!/bin/bash

# 一次性修复所有剩余编译错误的脚本
echo "开始批量修复所有剩余的编译错误..."

# 定义修复函数
fix_pagination_error() {
    local file=$1
    local entity_type=$2
    local mapper_name=$3
    
    echo "修复文件: $file"
    
    # 使用sed进行批量替换
    # 1. 在orderBy行后添加分页变量定义
    sed -i "/lambdaQueryWrapper\.orderBy.*$/a\\
        Page<$entity_type> page = new Page<>(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());\
        Page<$entity_type> pageResult = ${mapper_name}.selectPage(page, lambdaQueryWrapper);\
        List<$entity_type> dataList = pageResult.getRecords();" "$file"
    
    # 2. 替换PageImpl构造
    sed -i "s/PageImpl pageImpl = new PageImpl(dataList, pageRequest, pageResult\.getTotal());/PageRequest pageRequest = PageRequest.of(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());\
        PageImpl pageImpl = new PageImpl(dataList, pageRequest, pageResult.getTotal());/" "$file"
    
    # 3. 修复long到int的类型转换
    sed -i "s/paginationResponse\.setTotalPages(pageResult\.getPages());/paginationResponse.setTotalPages((int) pageResult.getPages());/" "$file"
    
    echo "✓ 修复完成: $file"
}

# 修复所有文件
fix_pagination_error "fuint-application/src/main/java/com/fuint/common/service/impl/CommissionRelationServiceImpl.java" "MtCommissionRelation" "mtCommissionRelationMapper"

fix_pagination_error "fuint-application/src/main/java/com/fuint/common/service/impl/BalanceServiceImpl.java" "MtBalance" "mtBalanceMapper"

fix_pagination_error "fuint-application/src/main/java/com/fuint/common/service/impl/MemberServiceImpl.java" "MtUser" "mtUserMapper"

fix_pagination_error "fuint-application/src/main/java/com/fuint/common/service/impl/OpenGiftServiceImpl.java" "MtOpenGift" "mtOpenGiftMapper"

fix_pagination_error "fuint-application/src/main/java/com/fuint/common/service/impl/CommissionCashServiceImpl.java" "MtCommissionCash" "mtCommissionCashMapper"

fix_pagination_error "fuint-application/src/main/java/com/fuint/common/service/impl/CommissionLogServiceImpl.java" "MtCommissionLog" "mtCommissionLogMapper"

echo ""
echo "所有文件修复完成！"
echo "正在进行编译测试..."

# 编译测试
cd fuintBackend
mvn compile -q

if [ $? -eq 0 ]; then
    echo "✅ 编译成功！所有错误已修复！"
else
    echo "❌ 编译仍有错误，需要手动检查"
fi
